cd "~/Dropbox/Shared Folder - J. Ladd & M (1). Meredith/Replication Files/"
cap log close
log using "LogFiles/FigureA4.log", replace

clear all
set more off

// Start Date for Scatter
local date1 = mdy(1, 1, 1953)
// End Date for Scatter
local date2 = mdy(1, 1, 2014)
// End Date for In Person Surveys
local date3 = mdy(1, 1, 1997)
// Start Date for Phone Surveys
local date4 = mdy(1, 1, 1989)

// Kernel Bandwidth
local bandwidthlevel = 100

use "RawData/GallupDataBJPS.dta", clear
// Drops Unrepresentative Series
drop if drops == "All" | regexm(drops, "Party")
// Drops Obs. w/o Gender
drop if female < 0
// Drops Obs. w/o Weights
drop if final_weight < 0
// Drops Obs. w/o Dem., Ind., or Rep.
keep if party == -1 | party == 0 | party == 1

// Sets the Survey Date as the Mid-Point in Field
gen middle_date = .
replace middle_date = floor((start_date + end_date) / 2)
sort middle_date
format middle_date %td

// Phone Poll Indicator
gen phone = (survey ~= 1)

// Codes Dem. as 0, Ind. as 1/2, and Rep. as 1
gen partisan = 0 if party == -1
replace partisan = 1 if party == 1
replace partisan = .5 if party == 0

// Weighted Female Republican Count
egen total_partisan_female_rep = sum((female == 1) * (partisan == 1) * final_weight) , by (series)
// Weighted Male Republican Count
egen total_partisan_male_rep = sum((female == 0) * (partisan == 1) * final_weight), by (series)
// Weighted Female Independent Count
egen total_partisan_female_ind = sum((female == 1) * (partisan == .5) * final_weight) , by (series)
// Weighted Male Independent Count
egen total_partisan_male_ind = sum((female == 0) * (partisan == .5) * final_weight), by (series)
// Weighted Female Count
egen total_obs_female = sum((female == 1) * (final_weight)), by (series)
// Weighted Male Count
egen total_obs_male = sum((female == 0) * (final_weight)), by (series)
// Avg. Female Republicanism
gen partisan_female_rep = total_partisan_female_rep / total_obs_female
// Avg. Male Republicanism
gen partisan_male_rep = total_partisan_male_rep / total_obs_male 
// Avg. Female Republican or Independent
gen partisan_female_rep_ind = (total_partisan_female_rep + total_partisan_female_ind) / total_obs_female
// Avg. Male Republican or Independent
gen partisan_male_rep_ind = (total_partisan_male_rep + total_partisan_male_ind) / total_obs_male

// Only Keeps One Obs. Per Survey
keep if obs_num == 1 

// Smoothed Male Republican In-Person
lpoly partisan_male_rep middle_date if phone == 0, /*
*/ degree(0) kernel(epanechnikov) bwidth (`bandwidthlevel') /*
*/ nograph at(middle_date) generate(male_rep_inperson_fitted)
// Smoothed Male Republican Phone 
lpoly partisan_male_rep middle_date if phone == 1, /*
*/ degree(0) kernel(epanechnikov) bwidth (`bandwidthlevel') /*
*/ nograph at(middle_date) generate(male_rep_phone_fitted)
// Smoothed Female Republican In-Person
lpoly partisan_female_rep middle_date if phone == 0, /*
*/ degree(0) kernel(epanechnikov) bwidth (`bandwidthlevel') /*
*/ nograph at(middle_date) generate(female_rep_inperson_fitted)
// Smoothed Female Republican Phone 
lpoly partisan_female_rep middle_date if phone == 1, /*
*/ degree(0) kernel(epanechnikov) bwidth (`bandwidthlevel') /*
*/ nograph at(middle_date) generate(female_rep_phone_fitted)
// Smoothed Male Republican or Independent In-Person
lpoly partisan_male_rep_ind middle_date if phone == 0, /*
*/ degree(0) kernel(epanechnikov) bwidth (`bandwidthlevel') /*
*/ nograph at(middle_date) generate(male_rep_ind_inperson_fitted)
// Smoothed Male Republican or Independent Phone 
lpoly partisan_male_rep_ind middle_date if phone == 1, /*
*/ degree(0) kernel(epanechnikov) bwidth (`bandwidthlevel') /*
*/ nograph at(middle_date) generate(male_rep_ind_phone_fitted)
// Smoothed Female Republican or Independent In-Person
lpoly partisan_female_rep_ind middle_date if phone == 0, /*
*/ degree(0) kernel(epanechnikov) bwidth (`bandwidthlevel') /*
*/ nograph at(middle_date) generate(female_rep_ind_inperson_fitted)
// Smoothed Female Republican or Independent Phone 
lpoly partisan_female_rep_ind middle_date if phone == 1, /*
*/ degree(0) kernel(epanechnikov) bwidth (`bandwidthlevel') /*
*/ nograph at(middle_date) generate(female_rep_ind_phone_fitted)

// Plots Fitted Values from Above Analysis
twoway (line male_rep_inperson_fitted middle_date if middle_date <= `date3', lpattern(solid) lcolor(gs8) lwidth(thick)) /*
*/ (line male_rep_phone_fitted middle_date if middle_date >= `date4', lpattern(shortdash) lcolor(gs8) lwidth(thick)) /*
*/ (line female_rep_inperson_fitted middle_date if middle_date <= `date3', lpattern(solid) lcolor(gs0) lwidth(thick)) /*
*/ (line female_rep_phone_fitted middle_date if middle_date >= `date4', lpattern(shortdash) lcolor(gs0) lwidth(thick)) /*
*/ (line male_rep_ind_inperson_fitted middle_date if middle_date <= `date3', lpattern(solid) lcolor(gs12) lwidth(thick)) /*
*/ (line male_rep_ind_phone_fitted middle_date if middle_date >= `date4', lpattern(shortdash) lcolor(gs12) lwidth(thick)) /*
*/ (line female_rep_ind_inperson_fitted middle_date if middle_date <= `date3', lpattern(solid) lcolor(gs4) lwidth(thick)) /*
*/ (line female_rep_ind_phone_fitted middle_date if middle_date >= `date4', lpattern(shortdash) lcolor(gs4) lwidth(thick)), /*
*/ legend(rows(4) order(1 "Male Rep. (In Person)" 2 "Male Rep. (Phone)" 3 "Female Rep. (In Person)" 4 "Female Rep. (Phone)" /*
*/ 5 "Male Rep. & Ind. (In Person)" 6 "Male Rep. & Ind. (Phone)" 7 "Female Rep. & Ind. (In Person)" 8 "Female Rep. & Ind. (Phone)")) /*
*/ xsize(14) ysize(8) scale(.8) xtitle({stSerif: Survey Date}) ytitle("{stSerif: Percent Affiliated}") /*  
*/ graphregion(fcolor(white)) xlabel(`date1'(1461)`date2', format(%td) alternate) ylabel(,nogrid) 

graph export "TablesFigures/FigureA4.eps", replace

log close
